﻿//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media;
using Expenses.ExpenseService;

namespace Expenses.ViewModels
{
    public class ChargeViewModel : ViewModelBase
    {
        #region Properties

        private int id = 0;
        public int Id
        {
            get
            { return id; }

            set
            {
                if (id == value)
                { return; }

                id = value;
                RaisePropertyChanged("Id");
            }
        }

        private int employeeId = 0;
        public int EmployeeId
        {
            get
            { return employeeId; }

            set
            {
                if (employeeId == value)
                { return; }

                employeeId = value;
                isDirty = true;
                RaisePropertyChanged("EmployeeId");
            }
        }

        private DateTime expenseDate = DateTime.Today;
        public DateTime ExpenseDate
        {
            get
            {
                return expenseDate;
            }

            set
            {
                if (expenseDate == value)
                { return; }

                expenseDate = value;
                isDirty = true;
                RaisePropertyChanged("ExpenseDate");
            }
        }

        private string merchant = string.Empty;
        public string Merchant
        {
            get
            { return merchant; }

            set
            {
                if (merchant == value)
                { return; }

                merchant = value;
                isDirty = true;
                RaisePropertyChanged("Merchant");
            }
        }

        private string location = string.Empty;
        public string Location
        {
            get
            { return location; }

            set
            {
                if (location == value)
                { return; }

                location = value;
                isDirty = true;
                RaisePropertyChanged("Location");
            }
        }

        private decimal billedAmount = 0M;
        public decimal BilledAmount
        {
            get
            { return billedAmount; }

            set
            {
                if (billedAmount == value)
                { return; }

                billedAmount = value;
                isDirty = true;
                RaisePropertyChanged("BilledAmount");
            }
        }

        private decimal transactionAmount = 0M;
        public decimal TransactionAmount
        {
            get
            { return transactionAmount; }

            set
            {
                if (transactionAmount == value)
                { return; }

                transactionAmount = value;
                isDirty = true;
                RaisePropertyChanged("TransactionAmount");
            }
        }

        private string description = string.Empty;
        public string Description
        {
            get
            { return description; }

            set
            {
                if (description == value)
                { return; }

                description = value;
                isDirty = true;
                RaisePropertyChanged("Description");
            }
        }

        private int category = 0;
        public int Category
        {
            get
            { return category; }

            set
            {
                if (category == value)
                { return; }

                category = value;
                isDirty = true;
                RaisePropertyChanged("Category");
            }
        }

        private List<string> categories;
        public List<string> Categories
        {
            get
            { return categories; }

            set
            {
                if (categories == value)
                { return; }

                categories = value;
                RaisePropertyChanged("Categories");
            }
        }

        private List<string> otherCategories;
        public List<string> OtherCategories
        {
            get
            { return otherCategories; }

            set
            {
                if (otherCategories == value)
                { return; }

                otherCategories = value;
                RaisePropertyChanged("OtherCategories");
            }
        }

        private int accountNumber = 0;
        public int AccountNumber
        {
            get
            { return accountNumber; }

            set
            {
                if (accountNumber == value)
                { return; }

                accountNumber = value;
                isDirty = true;
                RaisePropertyChanged("AccountNumber");
            }
        }

        private bool receiptRequired = false;
        public bool ReceiptRequired
        {
            get
            { return receiptRequired; }

            set
            {
                if (receiptRequired == value)
                { return; }

                receiptRequired = value;
                RaisePropertyChanged("ReceiptRequired");
            }
        }

        private bool isLate = false;
        public bool IsLate
        {
            get
            { return isLate; }

            set
            {
                if (isLate == value)
                { return; }

                isLate = value;
                RaisePropertyChanged("IsLate");
            }
        }

        private string foregroundColor;
        public string ForegroundColor
        {
            get
            {
                return foregroundColor;
            }

            set
            {
                foregroundColor = value;
                RaisePropertyChanged("ForegroundColor");
            }
        }

        private int expenseType = 0;
        public int ExpenseType
        {
            get
            { return expenseType; }

            set
            {
                if (expenseType == value)
                { return; }

                expenseType = value;
                isDirty = true;
                RaisePropertyChanged("ExpenseType");
            }
        }

        private string notes = string.Empty;
        public string Notes
        {
            get
            { return notes; }

            set
            {
                if (notes == value)
                { return; }

                notes = value;
                isDirty = true;
                RaisePropertyChanged("Notes");
            }
        }

        private bool isDirty = false;
        public bool IsDirty
        {
            get
            {
                return isDirty;
            }

            set
            {
                isDirty = value;
                RaisePropertyChanged("IsDirty");
            }
        }

        #endregion "Properties"

        public async Task<ChargeViewModel> GetCharge(int chargeNumber)
        {
            var expensesService = new ExpenseServiceClient();
            Charge charge = await
                expensesService.GetChargeAsync(chargeNumber);

            var chargeViewModel = new ChargeViewModel()
            {
                AccountNumber = Convert.ToInt32(charge.AccountNumber),
                BilledAmount = charge.BilledAmount,
                Category = Convert.ToInt32(charge.Category),
                Description = charge.Description,
                EmployeeId = charge.EmployeeId,
                ExpenseDate = charge.ExpenseDate,
                ExpenseType=charge.ExpenseType,
                Id = charge.Id,
                Location = charge.Location,
                Merchant = charge.Merchant,
                ReceiptRequired = charge.ReceiptRequired,
                TransactionAmount = charge.TransactionAmount,
                Notes = charge.Notes
            };
            return chargeViewModel;
        }

        public async Task<string> SaveCharge(ChargeViewModel _charge)
        {
            // Convert charge to Charge 
            var charge = new Charge
            {
                AccountNumber = Convert.ToInt32(_charge.AccountNumber),
                BilledAmount = _charge.BilledAmount,
                Category = Convert.ToInt32(_charge.Category),
                Description = _charge.Description,
                EmployeeId = _charge.EmployeeId,
                ExpenseDate = _charge.ExpenseDate,
                ExpenseType=_charge.ExpenseType,
                Id = _charge.Id,
                Location = _charge.Location,
                Merchant = _charge.Merchant,
                ReceiptRequired = _charge.ReceiptRequired,
                TransactionAmount = _charge.TransactionAmount,
                Notes = _charge.Notes
            };

            var expensesService = new ExpenseServiceClient();

            string result = await expensesService.SaveChargeAsync(
                charge.Id, charge);
            return result;
        }

        public async Task<string> DeleteCharge(int chargeNumber)
        {
            var expensesService = new ExpenseServiceClient();

            string result = await expensesService.DeleteChargeAsync(chargeNumber);
            return result;
        }

    }

}
